<template>
  <div class="app-container">
    <el-card v-show="showSearch" class="mb10" shadow="hover">
      <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="100px" size="small">
        <el-form-item label="油站" prop="params.stationId">
          <el-select v-model="queryParams.params.stationId" clearable placeholder="请选择油站">
            <el-option
              v-for="item in stationList"
              :key="item.id"
              :label="item.fullName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="会员昵称" prop="params.nickName">
          <el-input
            v-model="queryParams.params.nickName"
            clearable
            placeholder="请输入会员昵称"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="手机号码" prop="params.phone">
          <el-input
            v-model="queryParams.params.phone"
            clearable
            placeholder="请输入手机号码"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="会员码" prop="params.openid">
          <el-input
            v-model="queryParams.params.openid"
            clearable
            placeholder="请输入会员码"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="推荐人" prop="params.referrerOpenid">
          <el-input
            v-model="queryParams.params.referrerOpenid"
            clearable
            placeholder="请输入推荐人会员码"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="dateRange"
            :default-time="['00:00:00', '23:59:59']"
            end-placeholder="结束日期"
            range-separator="-"
            start-placeholder="开始日期"
            style="width: 100%"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
          ></el-date-picker>
        </el-form-item>
        <el-form-item class="ml10">
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card shadow="hover">
      <el-row :gutter="10" class="mb8">
        <!--        <el-col :span="1.5">
                  <el-button
                    v-hasPermi="['member:info:add']"
                    icon="el-icon-plus"
                    plain
                    size="mini"
                    type="primary"
                    @click="handleAdd"
                  >新增
                  </el-button>
                </el-col>-->
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['member:info:edit']"
            :disabled="single"
            icon="el-icon-edit"
            plain
            size="mini"
            type="success"
            @click="handleUpdate"
          >修改
          </el-button>
        </el-col>
        <!--        <el-col :span="1.5">
                  <el-button
                    v-hasPermi="['member:info:remove']"
                    :disabled="multiple"
                    icon="el-icon-delete"
                    plain
                    size="mini"
                    type="danger"
                    @click="handleDelete"
                  >删除
                  </el-button>
                </el-col>-->
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['member:info:export']"
            icon="el-icon-download"
            plain
            size="mini"
            type="warning"
            @click="handleExport"
          >导出
          </el-button>
        </el-col>
        <right-toolbar :columns="columns" :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="infoList" border @sort-change="handleSortChange"
                @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55"/>
        <el-table-column v-if="columns[0].visible" key="id" align="center" label="会员ID" prop="id"/>
        <el-table-column v-if="columns[1].visible" align="center" label="商户ID" prop="mchId"/>
        <el-table-column v-if="columns[2].visible" align="center" label="油站" prop="stationName"/>
        <el-table-column v-if="columns[3].visible" align="center" label="会员昵称" prop="nickName"
                         show-overflow-tooltip/>
        <el-table-column v-if="columns[4].visible" align="center" label="真实姓名" prop="realName"
                         show-overflow-tooltip/>
        <el-table-column v-if="columns[5].visible" align="center" label="用户头像" prop="avatar"/>
        <el-table-column v-if="columns[6].visible" align="center" label="性别" prop="sex">
          <template #default="scope">
            <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[7].visible" align="center" label="手机号码" prop="phone"/>
        <el-table-column v-if="columns[8].visible" align="center" label="邮箱地址" prop="email"/>
        <el-table-column v-if="columns[9].visible" align="center" label="收货地址" prop="address"/>
        <el-table-column v-if="columns[10].visible" align="center" label="注册日期" prop="registrationDate"
                         show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ formatDateTime(scope.row.registrationDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[11].visible" align="center" label="注册来源" prop="registrationSource"
                         show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="dict.type.registration_source" :value="scope.row.registrationSource"/>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[12].visible" align="center" label="首次消费日期" prop="firstConsumptionDate"
                         width="180">
          <template slot-scope="scope">
            <span>{{ formatDateTime(scope.row.firstConsumptionDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[13].visible" align="center" label="最新消费日期" prop="latestConsumptionDate"
                         width="180">
          <template slot-scope="scope">
            <span>{{ formatDateTime(scope.row.latestConsumptionDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[14].visible" align="center" label="会员码" prop="openid" show-overflow-tooltip/>
        <el-table-column v-if="columns[15].visible" align="center" label="unionid" prop="unionid"/>
        <el-table-column v-if="columns[16].visible" align="center" label="推荐人" prop="referrerOpenid"
                         show-overflow-tooltip/>
        <el-table-column v-if="columns[17].visible" align="center" label="经度" prop="longitude"/>
        <el-table-column v-if="columns[18].visible" align="center" label="纬度" prop="latitude"/>
        <el-table-column v-if="columns[19].visible" align="center" label="省份" prop="province"/>
        <el-table-column v-if="columns[20].visible" align="center" label="城市" prop="city"/>
        <el-table-column v-if="columns[21].visible" align="center" label="会员状态" prop="memberStatus">
          <template #default="scope">
            <dict-tag :options="dict.type.member_status" :value="scope.row.memberStatus"/>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[22].visible" align="center" label="会员类型" prop="memberType">
          <template #default="scope">
            <dict-tag :options="dict.type.member_type" :value="scope.row.memberType"/>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[23].visible" align="center" label="拉新类型" prop="recruitType"/>
        <el-table-column v-if="columns[24].visible" align="center" label="累计积分" prop="totalPoints"/>
        <el-table-column v-if="columns[25].visible" align="center" label="累计金币" prop="totalCoins"/>
        <el-table-column v-if="columns[26].visible" align="center" label="累计消费金额" prop="totalConsumptionAmount"/>
        <el-table-column v-if="columns[27].visible" align="center" label="累计优惠金额" prop="totalDiscountAmount"/>
        <el-table-column v-if="columns[28].visible" align="center" label="累计退款金额" prop="totalRefundAmount"/>
        <el-table-column v-if="columns[29].visible" align="center" label="累计消费次数" prop="totalConsumptionTimes"/>
        <el-table-column v-if="columns[30].visible" align="center" label="累计退款次数" prop="totalRefundTimes"/>
        <el-table-column v-if="columns[31].visible" align="center" label="三十天加油总升数" prop="fuelLitre30Day"/>
        <el-table-column v-if="columns[32].visible" align="center" label="三十天加油次数" prop="fuelCount30Day"/>
        <el-table-column v-if="columns[33].visible" align="center" label="六十天加油升数" prop="fuelLitre60Day"/>
        <el-table-column v-if="columns[34].visible" align="center" label="六十天加油次数" prop="fuelCount60Day"/>
        <el-table-column v-if="columns[35].visible" align="center" label="一百二十天加油升数" prop="fuelLitre120Day"/>
        <el-table-column v-if="columns[36].visible" align="center" label="一百二十天加油总数" prop="fuelCount120Day"/>
        <el-table-column v-if="columns[37].visible" align="center" label="创建人名称" prop="creatorName"/>
        <el-table-column v-if="columns[38].visible" align="center" label="创建时间" prop="createDate"
                         show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ formatDateTime(scope.row.createDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[39].visible" align="center" label="编辑时间" prop="modifiedDate"
                         show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ formatDateTime(scope.row.modifiedDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[40].visible" align="center" label="备注信息" prop="remark"
                         show-overflow-tooltip/>
        <el-table-column v-if="columns[41].visible" align="center" label="成长值" prop="levelPoint"/>
        <el-table-column v-if="columns[42].visible" align="center" label="会员等级" prop="memberLevel"/>
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
          <template slot-scope="scope">
            <el-tooltip content="修改" placement="top">
              <el-button
                v-hasPermi="['member:info:edit']"
                icon="el-icon-edit-outline"
                size="mini"
                type="text"
                @click="handleUpdate(scope.row)"/>
            </el-tooltip>

            <!--            <el-tooltip content="删除" placement="top">
                          <el-button
                            v-hasPermi="['member:info:remove']"
                            icon="el-icon-delete"
                            size="mini"
                            type="text"
                            @click="handleDelete(scope.row)"/>
                        </el-tooltip>-->
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :limit.sync="queryParams.pageSize"
        :page.sync="queryParams.pageNum"
        :total="total"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改会员基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="1200px">
      <el-form ref="form" :model="form" :rules="rules">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="会员昵称" prop="nickName">
              <el-input v-model="form.nickName" placeholder="请输入会员昵称"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="会员类型" prop="memberType">
              <el-select v-model="form.memberType" placeholder="请选择会员类型">
                <el-option
                  v-for="dict in dict.type.member_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="会员码" prop="openid">
              <el-input v-model="form.openid" placeholder="请输入会员码" readonly="false"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="推荐人会员码" prop="referrerOpenid">
              <el-input v-model="form.referrerOpenid" placeholder="请输入推荐人会员码" readonly="false"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="油站" prop="stationId">
              <el-select v-model="form.stationId" clearable placeholder="油站">
                <el-option
                  v-for="item in stationList"
                  :key="item.id"
                  :label="item.fullName"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="手机号码" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入手机号码"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="邮箱地址" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱地址"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注信息" prop="remark">
              <el-input v-model="form.remark" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入备注信息"
                        type="textarea"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addInfo, getInfo, listInfo, updateInfo} from "@/api/member/info";
import {formatDateTime, nowCnFormatDateTime} from "@/utils/date";
import {selectList} from "@/api/base/station";

export default {
  name: "Info",
  dicts: ['sys_user_sex', 'member_status', 'member_type', 'registration_source'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员基础信息表格数据
      infoList: [],
      // 油站信息
      stationList: [],
      // 日期范围
      dateRange: [],
      // 列信息
      columns: [
        {key: 0, label: `会员ID`, visible: true},
        {key: 1, label: `商户ID`, visible: false},
        {key: 2, label: `油站`, visible: true},
        {key: 3, label: `会员昵称`, visible: true},
        {key: 4, label: `真实姓名`, visible: false},
        {key: 5, label: `用户头像`, visible: false},
        {key: 6, label: `性别`, visible: true},
        {key: 7, label: `手机号码`, visible: true},
        {key: 8, label: `邮箱地址`, visible: false},
        {key: 9, label: `收货地址`, visible: false},
        {key: 10, label: `注册日期`, visible: true},
        {key: 11, label: `注册来源`, visible: true},
        {key: 12, label: `首次消费日期`, visible: false},
        {key: 13, label: `最新消费日期`, visible: false},
        {key: 14, label: `会员码`, visible: true},
        {key: 15, label: `unionid`, visible: false},
        {key: 16, label: `推荐人`, visible: false},
        {key: 17, label: `经度`, visible: false},
        {key: 18, label: `纬度`, visible: false},
        {key: 19, label: `省份`, visible: false},
        {key: 20, label: `城市`, visible: false},
        {key: 21, label: `会员状态`, visible: true},
        {key: 22, label: `会员类型`, visible: true},
        {key: 23, label: `拉新类型`, visible: false},
        {key: 24, label: `累计积分`, visible: false},
        {key: 25, label: `累计金币`, visible: false},
        {key: 26, label: `累计消费金额`, visible: false},
        {key: 27, label: `累计优惠金额`, visible: false},
        {key: 28, label: `累计退款金额`, visible: false},
        {key: 29, label: `累计消费次数`, visible: false},
        {key: 30, label: `累计退款次数`, visible: false},
        {key: 31, label: `三十天加油总升数`, visible: false},
        {key: 32, label: `三十天加油次数`, visible: false},
        {key: 33, label: `六十天加油升数`, visible: false},
        {key: 34, label: `六十天加油次数`, visible: false},
        {key: 35, label: `一百二十天加油升数`, visible: false},
        {key: 36, label: `一百二十天加油总数`, visible: false},
        {key: 37, label: `创建人名称`, visible: false},
        {key: 38, label: `创建时间`, visible: false},
        {key: 39, label: `编辑时间`, visible: false},
        {key: 40, label: `备注信息`, visible: false},
        {key: 41, label: `成长值`, visible: true},
        {key: 42, label: `会员等级`, visible: true}
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sortName: 'id',
        sortOrder: 'desc',
        params: {
          stationId: undefined,
          nickName: undefined,
          phone: undefined,
          openid: undefined,
          referrerOpenid: undefined
        }
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stationId: [
          {
            required: true, message: "油站ID不能为空", trigger: "blur"
          }
        ],
        nickName: [
          {
            required: true, message: "会员昵称不能为空", trigger: "blur"
          }
        ],
        sex: [
          {
            required: true, message: "性别不能为空", trigger: "change"
          }
        ],
        phone: [
          {
            required: true, message: "手机号码不能为空", trigger: "blur"
          }
        ],
        memberType: [
          {
            required: true, message: "会员类型不能为空", trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getStationList();
  },
  methods: {
    formatDateTime,
    /** 查询会员基础信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.infoList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    getStationList() {
      selectList().then(res => {
        this.stationList = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        mchId: undefined,
        stationId: undefined,
        nickName: undefined,
        realName: undefined,
        avatar: undefined,
        sex: undefined,
        phone: undefined,
        email: undefined,
        address: undefined,
        registrationDate: undefined,
        registrationSource: undefined,
        firstConsumptionDate: undefined,
        latestConsumptionDate: undefined,
        openid: undefined,
        unionid: undefined,
        referrerOpenid: undefined,
        longitude: undefined,
        latitude: undefined,
        province: undefined,
        city: undefined,
        memberStatus: undefined,
        memberType: undefined,
        recruitType: undefined,
        totalPoints: undefined,
        totalCoins: undefined,
        totalConsumptionAmount: undefined,
        totalDiscountAmount: undefined,
        totalRefundAmount: undefined,
        totalConsumptionTimes: undefined,
        totalRefundTimes: undefined,
        fuelLitre30Day: undefined,
        fuelCount30Day: undefined,
        fuelLitre60Day: undefined,
        fuelCount60Day: undefined,
        fuelLitre120Day: undefined,
        fuelCount120Day: undefined,
        creatorId: undefined,
        creatorName: undefined,
        createDate: undefined,
        modifierId: undefined,
        modifierName: undefined,
        modifiedDate: undefined,
        active: undefined,
        remark: undefined,
        version: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleSortChange(data) {
      console.log('handleSortChange')
      console.log(data)
      if (data.order) {
        if (data.order.indexOf('asc') > -1) {
          this.queryParams.sortOrder = 'asc'
        } else {
          this.queryParams.sortOrder = 'desc'
        }
        this.queryParams.sortName = data.prop
      }
      console.log(this.queryParams)
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改会员基础信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会员基础信息编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delInfo(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/member/info/export', {
        ...this.queryParams
      }, `会员信息_${nowCnFormatDateTime()}.xlsx`)
    }
  }
}

</script>

<style>
.el-form .el-select {
  width: 100%;
}
</style>
